using System.ComponentModel;
using System.Windows.Media.Imaging;
using Newtonsoft.Json;

namespace DocumentManager.Models;

/// <summary>
/// 内容项基类
/// </summary>
public class ContentItem : INotifyPropertyChanged
{
    private ContentType _contentType;
    private int _order;

    /// <summary>
    /// 内容项唯一标识
    /// </summary>
    public string Id { get; set; } = Guid.NewGuid().ToString();

    /// <summary>
    /// 内容类型
    /// </summary>
    public ContentType ContentType
    {
        get => _contentType;
        set
        {
            if (_contentType != value)
            {
                _contentType = value;
                OnPropertyChanged(nameof(ContentType));
            }
        }
    }

    /// <summary>
    /// 排序顺序
    /// </summary>
    public int Order
    {
        get => _order;
        set
        {
            if (_order != value)
            {
                _order = value;
                OnPropertyChanged(nameof(Order));
            }
        }
    }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedTime { get; set; } = DateTime.Now;

    /// <summary>
    /// 修改时间
    /// </summary>
    public DateTime ModifiedTime { get; set; } = DateTime.Now;

    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        ModifiedTime = DateTime.Now;
    }
}

/// <summary>
/// 文本内容项
/// </summary>
public class TextContentItem : ContentItem
{
    private string _text = string.Empty;
    private string _fontFamily = "微软雅黑";
    private double _fontSize = 12;
    private bool _isBold;
    private bool _isItalic;
    private string _textColor = "#000000";

    public TextContentItem()
    {
        ContentType = ContentType.Text;
    }

    /// <summary>
    /// 文本内容
    /// </summary>
    public string Text
    {
        get => _text;
        set
        {
            if (_text != value)
            {
                _text = value;
                OnPropertyChanged(nameof(Text));
            }
        }
    }

    /// <summary>
    /// 字体家族
    /// </summary>
    public string FontFamily
    {
        get => _fontFamily;
        set
        {
            if (_fontFamily != value)
            {
                _fontFamily = value;
                OnPropertyChanged(nameof(FontFamily));
            }
        }
    }

    /// <summary>
    /// 字体大小
    /// </summary>
    public double FontSize
    {
        get => _fontSize;
        set
        {
            if (Math.Abs(_fontSize - value) > 0.1)
            {
                _fontSize = value;
                OnPropertyChanged(nameof(FontSize));
            }
        }
    }

    /// <summary>
    /// 是否粗体
    /// </summary>
    public bool IsBold
    {
        get => _isBold;
        set
        {
            if (_isBold != value)
            {
                _isBold = value;
                OnPropertyChanged(nameof(IsBold));
            }
        }
    }

    /// <summary>
    /// 是否斜体
    /// </summary>
    public bool IsItalic
    {
        get => _isItalic;
        set
        {
            if (_isItalic != value)
            {
                _isItalic = value;
                OnPropertyChanged(nameof(IsItalic));
            }
        }
    }

    /// <summary>
    /// 文本颜色
    /// </summary>
    public string TextColor
    {
        get => _textColor;
        set
        {
            if (_textColor != value)
            {
                _textColor = value;
                OnPropertyChanged(nameof(TextColor));
            }
        }
    }
}

/// <summary>
/// 图片内容项
/// </summary>
public class ImageContentItem : ContentItem
{
    private string _imagePath = string.Empty;
    private byte[]? _imageData;
    private string _caption = string.Empty;
    private double _width;
    private double _height;
    private BitmapSource? _imageSource;

    public ImageContentItem()
    {
        ContentType = ContentType.Image;
    }

    /// <summary>
    /// 图片路径（相对路径或绝对路径）
    /// </summary>
    public string ImagePath
    {
        get => _imagePath;
        set
        {
            if (_imagePath != value)
            {
                _imagePath = value;
                OnPropertyChanged(nameof(ImagePath));
            }
        }
    }

    /// <summary>
    /// 图片二进制数据（用于嵌入图片）
    /// </summary>
    public byte[]? ImageData
    {
        get => _imageData;
        set
        {
            if (_imageData != value)
            {
                _imageData = value;
                OnPropertyChanged(nameof(ImageData));
            }
        }
    }

    /// <summary>
    /// 图片说明
    /// </summary>
    public string Caption
    {
        get => _caption;
        set
        {
            if (_caption != value)
            {
                _caption = value;
                OnPropertyChanged(nameof(Caption));
            }
        }
    }

    /// <summary>
    /// 显示宽度
    /// </summary>
    public double Width
    {
        get => _width;
        set
        {
            if (Math.Abs(_width - value) > 0.1)
            {
                _width = value;
                OnPropertyChanged(nameof(Width));
            }
        }
    }

    /// <summary>
    /// 显示高度
    /// </summary>
    public double Height
    {
        get => _height;
        set
        {
            if (Math.Abs(_height - value) > 0.1)
            {
                _height = value;
                OnPropertyChanged(nameof(Height));
            }
        }
    }

    /// <summary>
    /// 图片源（用于UI显示，不序列化）
    /// </summary>
    [JsonIgnore]
    public BitmapSource? ImageSource
    {
        get => _imageSource;
        set
        {
            if (_imageSource != value)
            {
                _imageSource = value;
                OnPropertyChanged(nameof(ImageSource));
            }
        }
    }

    /// <summary>
    /// 从BitmapSource设置图片数据
    /// </summary>
    public void SetImageFromBitmap(BitmapSource bitmapSource)
    {
        ImageSource = bitmapSource;
        Width = bitmapSource.PixelWidth;
        Height = bitmapSource.PixelHeight;

        // 转换为字节数组存储
        var encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
        using var stream = new System.IO.MemoryStream();
        encoder.Save(stream);
        ImageData = stream.ToArray();
    }

    /// <summary>
    /// 从字节数组加载图片源
    /// </summary>
    public void LoadImageFromData()
    {
        if (ImageData != null && ImageData.Length > 0)
        {
            try
            {
                var bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.StreamSource = new System.IO.MemoryStream(ImageData);
                bitmap.EndInit();
                ImageSource = bitmap;
            }
            catch (Exception ex)
            {
                // 图片数据损坏或格式不支持
                System.Diagnostics.Debug.WriteLine($"加载图片失败: {ex.Message}");
            }
        }
    }
} 